package com.mywang.weitravel.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.modules.ReadTypeBean;
import com.mywang.weitravel.modules.read.ReadChinaDetailActivity;
import com.mywang.weitravel.util.ImageLoader;

import java.util.List;

/**
 * Created by wangmingyun on 2019/12/11.
 */
public class ReadTypeAdapter extends RecyclerView.Adapter<ReadTypeAdapter.ReadTypeHolder> {

    private Context context;

    private List<ReadTypeBean> mDatas;

    public ReadTypeAdapter(Context context, List<ReadTypeBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public ReadTypeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_read_type, parent, false);
        return new ReadTypeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReadTypeHolder holder, int position) {
        ReadTypeBean bean = mDatas.get(position);
        ImageLoader.getInstance().displayImage(context, bean.getFilepath(), holder.ivPic);
        holder.tvTitle.setText(bean.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ReadChinaDetailActivity.class);
                intent.putExtra("id", bean.getId() + "");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ReadTypeHolder extends RecyclerView.ViewHolder {

        ImageView ivPic;
        TextView tvTitle;

        public ReadTypeHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.iv_pic);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }
}
